﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class AlbumList : BaseCtrl
    {
        int aBigID = YuRequest.GetQueryInt("BigID");
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                this.OnStart();
            }
        }

        /// <summary>
        /// 开始
        /// </summary>
        protected void OnStart()
        {
            Utils.WriteCookie("aBigID", aBigID.ToString(),10);            
            this.Pager.PageSize = 12;
            this.BindrptAlbum();
        }

        /// <summary>
        /// 绑定
        /// </summary>
        protected void BindrptAlbum()
        {
            int PageIndex = Pager.CurrentPageIndex;
            int PageSize = Pager.PageSize;
            int RecordCount = 0;
            rptAlbum.DataSource = bllalbum.GetList(aBigID, PageIndex, PageSize, out RecordCount);
            rptAlbum.DataBind();
            Pager.RecordCount = RecordCount;    
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptAlbum_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.BindrptAlbum();
        }

        /// <summary>
        /// 删除当前照片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "del")
            {
                int id = Utils.StrToInt(e.CommandArgument.ToString(), 0);
                if (id != 0)
                {                   
                    
                    Yu.Cms.Model.album model = bllalbum.GetModel(id);
                    int BigID = model.alBigID;
                    if (bllalbumType.isExistSmalID(BigID, id)) //更新封面
                    {
                        Yu.Cms.Model.albumType fam = bllalbumType.GetModel(BigID);
                        fam.alTopID = 0;
                        fam.alBigID = BigID;
                        bllalbumType.Update(fam);                       
                    }
                    bllalbum.Delete(id);
                    try
                    {
                        string sfilePath = Utils.GetRootPath(); //文件路径   
                        FSO.Delete(sfilePath + model.alPath, FsoMethod.File);
                        FSO.Delete(sfilePath + model.alThumbPath, FsoMethod.File);
                    }
                    catch
                    {
                        //删除，不抛出异常
                    }
                    this.BindrptAlbum(); 
                }
            }
        }

        protected void btnIndex_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "index")
            {
                int id = Utils.StrToInt(e.CommandArgument.ToString(), 0);
                if (id != 0)
                {
                    Yu.Cms.Model.album model = bllalbum.GetModel(id);
                    int BigID = model.alBigID;
                    Yu.Cms.Model.albumType tmodel = bllalbumType.GetModel(BigID);
                    tmodel.alTopID = id;
                    bllalbumType.Update(tmodel);
                }
                this.BindrptAlbum(); 
            }
        }
    }
}